473,480 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Showing a context menu during drag and drop

Hi All,

I'm trying to show a context menu during a drag drop operation similar
to the windows explorers right click drag and drop behavior (A full
working sample is at the end of the post):

void treeView1_DragDrop(object sender, DragEventArgs e)
{
ContextMenu mnu = new ContextMenu();
mnu.MenuItems.Add("Click me", OnCmClick);
mnu.Show(this, PointToClient(MousePosition));
}
private void OnCmClick(object sender, EventArgs e)
{
Debug.WriteLine("Click");
}

It works, but the OnCmClick handler of the context menu is executed
*after* the drag and drop has finished. I think this is because the
click message is enqueued into the message queue behind the currently
active drag and drop handler, as a call to Application.DoEvents() solves
the problem.

As I'm burned by re entrance issues, I'm very reluctant to call DoEvents.

Is there any other way to solve this issue or to implement this behavior?
Here's a complete sample:

using System;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace WindowsApplication4
{
public partial class Form1 : Form
{
private readonly TreeView treeView1 = new TreeView();

public Form1()
{
InitializeComponent();
Thread.CurrentThread.Name = "GUI";
treeView1.Nodes.Add("A Node");
treeView1.ItemDrag += treeView1_ItemDrag;
treeView1.DragOver += treeView1_DragOver;
treeView1.DragDrop += treeView1_DragDrop;
}

private void InitializeComponent()
{
SuspendLayout();
treeView1.AllowDrop = true;
treeView1.Location = new Point(10, 10);
treeView1.Name = "treeView1";
treeView1.Size = new Size(120, 200);
treeView1.TabIndex = 0;
ClientSize = new Size(300, 250);
Controls.Add(treeView1);
Name = "Form1";
Text = "Form1";
ResumeLayout(false);
}

private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
Debug.WriteLine("--->Started DragDrop");
Debug.WriteLine("Thread: " + Thread.CurrentThread.Name);
DoDragDrop(e.Item, DragDropEffects.All | DragDropEffects.Link);
Debug.WriteLine("--->Finished DragDrop");
}

private void treeView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}

private void treeView1_DragDrop(object sender, DragEventArgs e)
{
Debug.WriteLine("Thread: " + Thread.CurrentThread.Name);
ContextMenu mnu = new ContextMenu();
mnu.MenuItems.Add("Click me", OnCmClick);
mnu.Show(this, PointToClient(MousePosition));
//Application.DoEvents();// Good Idea??
}

private void OnCmClick(object sender, EventArgs e)
{
Debug.WriteLine("Click");
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}
}

Thank you in advance,
Andy
--
You can email me by removing the NOSPAM parts below:
xm**********@gmxNOSPAM.net
Sep 18 '07 #1
2 5258
Why not use a timer with a small interval of say 100ms and call the Show
method in timer.tick?

---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
---------
"Andreas Mueller" <me@privacy.netwrote in message
news:5l************@mid.individual.net...
Hi All,

I'm trying to show a context menu during a drag drop operation similar to
the windows explorers right click drag and drop behavior (A full working
sample is at the end of the post):

void treeView1_DragDrop(object sender, DragEventArgs e)
{
ContextMenu mnu = new ContextMenu();
mnu.MenuItems.Add("Click me", OnCmClick);
mnu.Show(this, PointToClient(MousePosition));
}
private void OnCmClick(object sender, EventArgs e)
{
Debug.WriteLine("Click");
}

It works, but the OnCmClick handler of the context menu is executed
*after* the drag and drop has finished. I think this is because the click
message is enqueued into the message queue behind the currently active
drag and drop handler, as a call to Application.DoEvents() solves the
problem.

As I'm burned by re entrance issues, I'm very reluctant to call DoEvents.

Is there any other way to solve this issue or to implement this behavior?
Here's a complete sample:

using System;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace WindowsApplication4
{
public partial class Form1 : Form
{
private readonly TreeView treeView1 = new TreeView();

public Form1()
{
InitializeComponent();
Thread.CurrentThread.Name = "GUI";
treeView1.Nodes.Add("A Node");
treeView1.ItemDrag += treeView1_ItemDrag;
treeView1.DragOver += treeView1_DragOver;
treeView1.DragDrop += treeView1_DragDrop;
}

private void InitializeComponent()
{
SuspendLayout();
treeView1.AllowDrop = true;
treeView1.Location = new Point(10, 10);
treeView1.Name = "treeView1";
treeView1.Size = new Size(120, 200);
treeView1.TabIndex = 0;
ClientSize = new Size(300, 250);
Controls.Add(treeView1);
Name = "Form1";
Text = "Form1";
ResumeLayout(false);
}

private void treeView1_ItemDrag(object sender, ItemDragEventArgs
e)
{
Debug.WriteLine("--->Started DragDrop");
Debug.WriteLine("Thread: " + Thread.CurrentThread.Name);
DoDragDrop(e.Item, DragDropEffects.All |
DragDropEffects.Link);
Debug.WriteLine("--->Finished DragDrop");
}

private void treeView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}

private void treeView1_DragDrop(object sender, DragEventArgs e)
{
Debug.WriteLine("Thread: " + Thread.CurrentThread.Name);
ContextMenu mnu = new ContextMenu();
mnu.MenuItems.Add("Click me", OnCmClick);
mnu.Show(this, PointToClient(MousePosition));
//Application.DoEvents();// Good Idea??
}

private void OnCmClick(object sender, EventArgs e)
{
Debug.WriteLine("Click");
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}
}

Thank you in advance,
Andy
--
You can email me by removing the NOSPAM parts below:
xm**********@gmxNOSPAM.net

Sep 19 '07 #2
G Himangi wrote:
Why not use a timer with a small interval of say 100ms and call the Show
method in timer.tick?
Hi,

the context menu itself shows up fine. The problem is that when I click
on the menu, the handler is executed *after* the Drag and Drop operation
has finished.

Thanks,
Andy
--
You can email me by removing the NOSPAM parts below:
xm**********@gmxNOSPAM.net
Sep 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3116
by: Francesco | last post by:
Hello Pythonnian's (sorry for crossposting) I have written on the base of Miro Rajic a (still) small filemanager in wxPython. Now I want to add (for Windows XP) a explorer shell context menu....
2
1393
by: Michel | last post by:
I like to show a inbetween selfmade menu when rightclicking the page. So on the first line I want to add "Default right-click menu". So first I need to capture it, then I need to stop the...
2
4272
by: Paul Ledger | last post by:
I want to be able to display a context menu when the user righthand drags an item into a textbox. It states in the documentation that the e.KeyState property will be set accordingly in the drop...
0
921
by: gurjinder | last post by:
Hi, I am making one component in that i am inherting from System.ComponentModel.Component i am trying to make a component like sqldataadapter . when we drag that component to our form in...
3
1971
by: Marcel Hug | last post by:
Hello NG ! I would like to show the context menu which apears when i drop a file by fight mouse button. Could somebody help me ? Thanks
0
934
by: Alvo von Cossel I | last post by:
hi, i have a context menu with the Drop-shadow propery set to false. it works for the main part of the menu but in a submenu it doesnt. is there any way to stop this apart from creating another...
1
1313
by: cool_jaggs | last post by:
Hi , I am new to this group, but it would be really nice if you can really help me out. I am trying to Export a file from my application to windows explorer through drag and drop using right...
1
4442
by: avanti | last post by:
Hi, I am trying to show a context menu on a right click in some of my controls. I want to show it at the right location. However it is getting shown away from the mouse click location. Here is...
0
1376
by: arvinds | last post by:
I have an application in which I have 2 forms. 1.Review Form 2. FilmForm. Review Form is used for Loading some images and we can transfer the Images from Review From to FilmForm by Drag-Drop...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6912
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.